package com.me.test.controller;

import com.me.test.util.FileUtil;
import com.me.test.util.S3ToolUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

@RestController
public class FileUploadController {

    @GetMapping("/upload")
    public String handleFileUpload() throws IOException {
        //免费测试网速 https://www.browserscan.net/zh/speed-test
        String accessKeyId="6f5bae54990710b267d95d1e08952c32";
        //S3加密访问密钥
        String secretAccessKey="102cbc31896c903e3424dd80d5af0bedf13e50751f03c7cd3117394cbc549789";
        //服务端点
        String endPoint="https://4bfa77a07e21f3293450a09392978a83.r2.cloudflarestorage.com";
        //桶名
        String bucketName="haishen-test";
        S3ToolUtils s3ToolUtils = new S3ToolUtils(accessKeyId,secretAccessKey,endPoint,bucketName);
        /*InputStream ins = s3ToolUtils.downloadFile("20140611165023_eBALy.jpeg");

        OutputStream os = new FileOutputStream(new File("E:\\haishen\\jingcw.jpeg"));
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.close();
        ins.close();*/
        long start = System.currentTimeMillis();
        //for (int i=10;i<18;i++){
        String fileurl = "https://haishen-test.oss-cn-beijing.aliyuncs.com/admin/2024/4/msdia80.dll";
        //File file = FileUtil.urlToFile("https://haishen-test.oss-cn-beijing.aliyuncs.com/admin/2024/4/msdia80.dll");
        //s3ToolUtils.uploadFile(file,"abcd/haishen/"+1+"msdia80.dll");
        URL url1 = new URL(fileurl);
        s3ToolUtils.uploadFileByStream(url1.openStream(),"abcd/haishen/"+1+"msdia80.dll");
        //}


        /*String fileurl = "https://haishen-test.oss-cn-beijing.aliyuncs.com/admin/2024/4/20241017133314_679919.png";
        URL url1 = new URL(fileurl);
        //File file = new File("E:\\haishen\\msdia80.dll");
        InputStream inputStream = url1.openStream();
        System.out.println("inputstream:"+(System.currentTimeMillis()-millis));
        s3ToolUtils.uploadFileByStream(inputStream,"abcd/haishen/20241017133314_679919.png");*/
        long hs = System.currentTimeMillis()- start;
        System.out.println("upload:"+hs);

        return "ok ： " +hs;
    }
}